package com.gjzt.business.customer.service;


import com.gjzt.business.customer.inter.ProductionCompanyMapper;
import com.gjzt.business.customer.model.ProductionCompany;
import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.StringUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductionCompanyService extends BaseService {

    @Autowired
    private ProductionCompanyMapper productionCompanyMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONObject findAdProductionCompany(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("", new Object[0]);
        return this.queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addAdProductionCompany(ReqBean reqBean) {
        ProductionCompany productionCompany = getAdProductionCompany(reqBean);

        productionCompanyMapper.insert(productionCompany);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateAdProductionCompany(ReqBean reqBean) {
        String companyId = reqBean.getInfo("companyId");
        if (StringUtil.isEmpty(companyId)) {
            throw new BusinessException("获取主键失败");
        }
        ProductionCompany productionCompany = getAdProductionCompany(reqBean);
        productionCompanyMapper.updateByPrimaryKeySelective(productionCompany);
    }

    /**
     * 删除
     *
     * @param reqBean
     */
    public void removeAdProductionCompany(ReqBean reqBean) {
        String companyId = reqBean.getInfo("companyId");
        if (StringUtil.isEmpty(companyId)) {
            throw new BusinessException("获取主键失败");
        }
        productionCompanyMapper.deleteByPrimaryKey(companyId);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private ProductionCompany getAdProductionCompany(ReqBean reqBean) {
        ProductionCompany productionCompany = new ProductionCompany();

        productionCompany.setCompanyId(reqBean.getInfo("companyId"));
        productionCompany.setCompanyName(reqBean.getInfo("companyName"));
        productionCompany.setCompanyCredNo(reqBean.getInfo("companyCredNo"));
        productionCompany.setContacts(reqBean.getInfo("contacts"));
        productionCompany.setContactsNumber(reqBean.getInfo("contactsNumber"));
        productionCompany.setAddress(reqBean.getInfo("address"));
        productionCompany.setRemark(reqBean.getInfo("remark"));

        return productionCompany;
    }

}